package com.erlinyou.im.service.chat.impl;

import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erlinyou.base.service.BaseServiceImpl;
import com.erlinyou.im.dao.repository.chat.ChatCurrwinRepository;
import com.erlinyou.im.dao.repository.chat.ChatSessionRepository;
import com.erlinyou.im.model.chat.ChatCurrwin;
import com.erlinyou.im.service.chat.IChatCurrwinService;
@Service
public class ChatCurrwinServiceImpl extends BaseServiceImpl<ChatCurrwin, Long> implements IChatCurrwinService {

	@Resource
	private ChatSessionRepository chatSessionRepository;
	
	private ChatCurrwinRepository repository;
	@Resource
	public void setBaseService(ChatCurrwinRepository repository) {
		this.repository = repository;
		this.baseRepository = repository;
	}
	
	@Override
	public <S extends ChatCurrwin> S save(S entity) {
		return repository.save(entity);
	}

	@Override
	public Optional<ChatCurrwin> findById(Long id) {
		return repository.findById(id);
	}

	@Override
	public boolean existsById(Long id) {
		return repository.existsById(id);
	}

	@Override
	public void deleteById(Long id) {
		repository.deleteById(id);
	}

	@Override
	public void delete(ChatCurrwin entity) {
		repository.delete(entity);
	}

	@Override
	public <S extends ChatCurrwin> boolean exists(Example<S> example) {
		return repository.exists(example);
	}


	@Override
	public int deleteByUserid(Long userid) {
		return repository.deleteByUserid(userid);
	}

	@Transactional
	@Override
	public void setCurrwinAndCleanReadNum(ChatCurrwin chatCurrwin) {
		if(repository.save(chatCurrwin)!=null){
			chatSessionRepository.cleanNoReadNum(chatCurrwin.getUserid(), chatCurrwin.getRid(),chatCurrwin.getCtype());
			return;
		}
		throw new RuntimeException();
	}

}
